//
//  LogonViewBuilder.m
//  Rongcloud
//
//  Created by LiuLinhong on 2017/09/04.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "LogonViewBuilder.h"
#import "LogonViewController.h"
#import "DJSwitchControl.h"

@interface LogonViewBuilder ()

@property (nonatomic, strong) LogonViewController *logonViewController;

@end

@implementation LogonViewBuilder

- (instancetype)initWithViewController:(NSViewController *)vc
{
    self = [super init];
    if (self)
    {
        self.logonViewController = (LogonViewController *) vc;
        [self initLogonView];
        [self initOptionTableView];
        [self initCopyRightView];
    }
    return self;
}

- (void)initLogonView
{
    self.backgroundImageView = [[NSImageView alloc] initWithFrame:self.logonViewController.logonMainView.frame];
    self.backgroundImageView.image = [NSImage imageNamed:@"LogonBackgroundImage"];
    [self.logonViewController.logonMainView addSubview:self.backgroundImageView];
    self.logonViewController.logonMainView.wantsLayer = YES;
//    self.backgroundImageView.layer.backgroundColor = [NSColor greenColor].CGColor;
    self.backgroundImageView.imageScaling = NSImageScaleAxesIndependently;
    
    //登录内容
    CGFloat logonContentViewHeight = 410;
    self.logonContentView = [[NSView alloc] initWithFrame:CGRectMake((self.logonViewController.view.frame.size.width - 400) /2, (self.logonViewController.view.frame.size.height - logonContentViewHeight) * 0.618, 400, logonContentViewHeight)];
    self.logonContentView.wantsLayer = YES;
    self.logonContentView.layer.backgroundColor = kLogonContentColor.CGColor;
    self.logonContentView.layer.masksToBounds = NO;
//    self.logonContentView.layer.cornerRadius = 5.0;
    self.logonContentView.layer.shadowOffset = CGSizeMake(0, 0);
    self.logonContentView.layer.shadowRadius = 10.0;
    self.logonContentView.layer.shadowOpacity = 1.0;
    self.logonContentView.layer.shadowColor = [NSColor colorWithRed:25.0/255.0 green:25.0/255.0 blue:25.0/255.0 alpha:0.7].CGColor;
    [self.logonViewController.logonMainView addSubview:self.logonContentView];
    
    //logo
    NSImageView *logoImageView = [[NSImageView alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 80) /2, self.logonContentView.frame.size.height - 80 - 30, 80, 80)];
    logoImageView.wantsLayer = YES;
    logoImageView.layer.cornerRadius = 5.0;
    logoImageView.image = [NSImage imageNamed:@"AppIcon"];
    [self.logonContentView addSubview:logoImageView];
    
    //欢迎体验
    NSTextField *welcomeTextField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 220) /2, logoImageView.frame.origin.y - 20 - 20, 220, 20)];
    welcomeTextField.stringValue = NSLocalizedString(@"logon_welcome_webrtc_av_meeting", nil);
    welcomeTextField.bordered = NO;
    welcomeTextField.editable = NO;
    welcomeTextField.selectable = NO;
    welcomeTextField.alignment = NSTextAlignmentCenter;
    welcomeTextField.font = [NSFont systemFontOfSize:14.0];
    welcomeTextField.textColor = kLogonTextColor;
    welcomeTextField.backgroundColor = [NSColor clearColor];
    [self.logonContentView addSubview:welcomeTextField];
    
    //RongRTCTalk V1.0.0
    self.versionTextField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 280) /2, welcomeTextField.frame.origin.y - 20, 280, 20)];
    self.versionTextField.stringValue = APP_Version;
    self.versionTextField.bordered = NO;
    self.versionTextField.editable = NO;
    self.versionTextField.alignment = NSTextAlignmentCenter;
    self.versionTextField.font = [NSFont systemFontOfSize:14.0];
    self.versionTextField.textColor = kLogonTextColor;
    self.versionTextField.backgroundColor = [NSColor clearColor];
    [self.logonContentView addSubview:self.versionTextField];
    
    //请输入会议室名称
    NSTextField *meetingRoomField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 220) /2, self.versionTextField.frame.origin.y - 24, 220, 20)];
    meetingRoomField.stringValue = NSLocalizedString(@"logon_input_meeting_room_name", nil);
    meetingRoomField.bordered = NO;
    meetingRoomField.editable = NO;
    meetingRoomField.alignment = NSTextAlignmentCenter;
    meetingRoomField.font = [NSFont systemFontOfSize:14.0];
    meetingRoomField.textColor = [NSColor colorWithRed:45.0/255.0 green:46.0/255.0 blue:45.0/255.0 alpha:1.0];
    meetingRoomField.backgroundColor = [NSColor clearColor];
    [self.logonContentView addSubview:meetingRoomField];
    
    //房间号
    self.channelIDTextField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 300) / 2, meetingRoomField.frame.origin.y - 35, 300, 30)];
    self.channelIDTextField.bezelStyle = NSTextFieldRoundedBezel;
    self.channelIDTextField.alignment = NSTextAlignmentCenter;
    self.channelIDTextField.font = [NSFont systemFontOfSize:16.0];
    self.channelIDTextField.target = self.logonContentView;
    self.channelIDTextField.action = @selector(roomNumberTextFieldDidChange:);
    self.channelIDTextField.delegate = self.logonViewController;
    [self.logonContentView addSubview:self.channelIDTextField];
    
    //请输入姓名
    NSTextField *inputNameField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 220) /2, self.channelIDTextField.frame.origin.y - 24, 220, 20)];
    inputNameField.stringValue = NSLocalizedString(@"logon_input_meeting_nick_name", nil);
    inputNameField.bordered = NO;
    inputNameField.editable = NO;
    inputNameField.alignment = NSTextAlignmentCenter;
    inputNameField.font = [NSFont systemFontOfSize:14.0];
    inputNameField.textColor = [NSColor colorWithRed:45.0/255.0 green:46.0/255.0 blue:45.0/255.0 alpha:1.0];
    inputNameField.backgroundColor = [NSColor clearColor];
    [self.logonContentView addSubview:inputNameField];
    
    //姓名
    self.userNameTextField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 300) / 2, inputNameField.frame.origin.y - 35, 300, 30)];
    self.userNameTextField.placeholderString = NSLocalizedString(@"logon_support_language", nil);
    self.userNameTextField.bezelStyle = NSTextFieldRoundedBezel;
    self.userNameTextField.alignment = NSTextAlignmentCenter;
    self.userNameTextField.font = [NSFont systemFontOfSize:14.0];
    self.userNameTextField.target = self.logonContentView;
    self.userNameTextField.action = @selector(roomNumberTextFieldDidChange:);
    self.userNameTextField.delegate = self.logonViewController;
    [self.logonContentView addSubview:self.userNameTextField];
    
    //Header Image
    NSImageView *headerImageView = [[NSImageView alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 300) / 2, self.userNameTextField.frame.origin.y - 20 - 5, 11, 20)];
    headerImageView.image = [NSImage imageNamed:@"LogonHeader"];
//    [self.logonContentView addSubview:headerImageView];
    
    //身份选项
    NSTextField *idChoiceTextField = [[NSTextField alloc] initWithFrame:CGRectMake(headerImageView.frame.origin.x, headerImageView.frame.origin.y, 120, 20)];
    idChoiceTextField.stringValue = NSLocalizedString(@"logon_id_option", nil);
    idChoiceTextField.bordered = NO;
    idChoiceTextField.editable = NO;
    idChoiceTextField.alignment = NSTextAlignmentLeft;
    idChoiceTextField.font = [NSFont systemFontOfSize:14.0];
    idChoiceTextField.textColor = kLogonTextColor;
    idChoiceTextField.backgroundColor = [NSColor clearColor];
    [self.logonContentView addSubview:idChoiceTextField];
    
    //以旁听人身份加入
    NSTextField *observerTextField = [[NSTextField alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 300) / 2, idChoiceTextField.frame.origin.y - 24, 300, 20)];
    observerTextField.bordered = NO;
    observerTextField.editable = NO;
    observerTextField.bezelStyle = NSTextFieldRoundedBezel;
    observerTextField.alignment = NSTextAlignmentLeft;
    observerTextField.stringValue = NSLocalizedString(@"logon_meeting_observer", nil);
    observerTextField.font = [NSFont systemFontOfSize:14.0];
    observerTextField.textColor = [NSColor blackColor];//[NSColor colorWithRed:160.0/255.0 green:160.0/255.0 blue:160.0/255.0 alpha:1.0];
    observerTextField.backgroundColor = [NSColor clearColor];
    [self.logonContentView addSubview:observerTextField];
    
    observerTextField.wantsLayer = YES;
    observerTextField.layer.backgroundColor = [NSColor whiteColor].CGColor;
    observerTextField.layer.cornerRadius = 5.0;
    
    //身份Switch
    self.identitySwitch = [[DJSwitchControl alloc] initWithFrame:CGRectMake(observerTextField.frame.size.width - 30 - 10, 2, 30, 16)];
    self.identitySwitch.target = self.logonViewController;
    self.identitySwitch.action = @selector(identitySwitchAction:);
    [observerTextField addSubview:self.identitySwitch];

    if (self.logonViewController.observerIndex == RongRTC_User_Observer)
        [self.identitySwitch setOn:YES];
    else if (self.logonViewController.observerIndex == RongRTC_User_Normal)
        [self.identitySwitch setOn:NO];
    
    //加入会议
    self.joinRoomButton = [[NSButton alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 180) / 2, observerTextField.frame.origin.y - 45, 180, 40)];
    self.joinRoomButton.title = NSLocalizedString(@"logon_join_channel", nil);
    self.joinRoomButton.font = [NSFont systemFontOfSize:14.0];
    self.joinRoomButton.bezelStyle = NSBezelStyleRounded;
    [(NSButtonCell *)self.joinRoomButton.cell setHighlightsBy:NSPushInCellMask];
    [self.joinRoomButton setTarget:self.logonViewController];
    [self.joinRoomButton setAction:@selector(joinRoomButtonPressed)];
    [self.logonContentView addSubview:self.joinRoomButton];
    
    //会议选项
    self.expandButton = [[NSButton alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 100) / 2, self.joinRoomButton.frame.origin.y - 28, 100, 30)];
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:14], NSFontAttributeName,
                            kLogonTextColor, NSForegroundColorAttributeName,
                            nil];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"logon_meeting_option_down", nil) attributes:attrs];
    [attributedString setAlignment:NSTextAlignmentCenter range:NSMakeRange(0, [attributedString length])];
    [self.expandButton setAttributedTitle:attributedString];
    self.expandButton.bezelStyle = NSBezelStyleRounded;
    self.expandButton.bordered = NO;
    [(NSButtonCell *)self.expandButton.cell setHighlightsBy:NSPushInCellMask];
    [self.expandButton setTarget:self.logonViewController];
    [self.expandButton setAction:@selector(expandButtonPressed)];
    [self.logonContentView addSubview:self.expandButton];
}

- (void)initOptionTableView
{
    //选项
    self.logonOptionView = [[NSView alloc] initWithFrame:CGRectMake(self.logonContentView.frame.origin.x, self.logonContentView.frame.origin.y - kExpandedPixel, self.logonContentView.frame.size.width, kExpandedPixel)];
    self.logonOptionView.wantsLayer = YES;
    self.logonOptionView.layer.backgroundColor = kLogonContentColor.CGColor;
    self.logonOptionView.layer.masksToBounds = NO;
    //    self.logonOptionView.layer.cornerRadius = 5.0;
    //    self.logonOptionView.layer.shadowOffset = CGSizeMake(0, 0);
    //    self.logonOptionView.layer.shadowRadius = 10.0;
    //    self.logonOptionView.layer.shadowOpacity = 0.8;
    //    self.logonOptionView.layer.shadowColor = [NSColor blackColor].CGColor;
    //    [self.backgroundImageView addSubview:self.logonOptionView];
    
    NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:CGRectMake((self.logonContentView.frame.size.width - 300) / 2, 10, 300, kExpandedPixel - 10)];
    scrollView.hasVerticalScroller = NO;
//    [scrollView setWantsLayer:YES];
//    [scrollView.layer setBackgroundColor:[NSColor greenColor].CGColor];
    scrollView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    [self.logonOptionView addSubview:scrollView];
    
    self.optionTableView = [[NSTableView alloc] init];
    self.optionTableView.frame = CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height);
    self.optionTableView.focusRingType = NSFocusRingTypeNone;
    self.optionTableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;
    self.optionTableView.headerView.frame = NSZeroRect;
    self.optionTableView.gridStyleMask = NSTableViewSolidHorizontalGridLineMask;
    self.optionTableView.usesAlternatingRowBackgroundColors = NO;
    self.optionTableView.allowsMultipleSelection = NO;
    self.optionTableView.delegate = self.logonViewController.logonOptionTableViewDelegateSource;
    self.optionTableView.dataSource = self.logonViewController.logonOptionTableViewDelegateSource;
    self.optionTableView.target = self.logonViewController;
    
//    [self.optionTableView setAction:@selector(selectClick:)];
    [self.optionTableView registerNib:[[NSNib alloc] initWithNibNamed:@"LogonOptionTableViewCellView" bundle:nil] forIdentifier:@"LogonOptionTableViewCellView"];
    scrollView.contentView.documentView = self.optionTableView;
    
    NSTableColumn *columen1 = [[NSTableColumn alloc] initWithIdentifier:@"columen1"];
    columen1.minWidth = 300.0;
    columen1.resizingMask = NSTableColumnUserResizingMask;
    [self.optionTableView addTableColumn:columen1];
}

- (void)initCopyRightView
{
    NSTextField *copyRightTextField = [[NSTextField alloc] initWithFrame:CGRectMake(0, 0, self.backgroundImageView.frame.size.width, 18)];
    copyRightTextField.bordered = NO;
    copyRightTextField.editable = NO;
    copyRightTextField.bezelStyle = NSTextFieldRoundedBezel;
    copyRightTextField.alignment = NSTextAlignmentCenter;
    copyRightTextField.stringValue = @"Beijing Rongcloud Network Technology Co. , Ltd © Copyright RongcloudMeeting";
    copyRightTextField.font = [NSFont systemFontOfSize:12.0];
    copyRightTextField.textColor = [NSColor blackColor];
//    [self.backgroundImageView addSubview:copyRightTextField];
    
    copyRightTextField.wantsLayer = YES;
    copyRightTextField.layer.backgroundColor = [NSColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.6].CGColor;
}

@end
